package com.timemanager.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.timemanager.R;
import com.timemanager.db.TimeManagerDB;
import com.timemanager.layout.TitleLayout;
import com.timemanager.model.Event;

public class AddEventActivity2 extends AppCompatActivity {

    private TextView titleText;
    private EditText eventNameEdit;
    private Button saveEvent;
    private String selectedType;
    private String getEditName;
    private Event event = new Event();
    private TimeManagerDB timeManagerDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        selectedType = getIntent().getStringExtra("selected_type");
        setContentView(R.layout.activity_add_event2);
        eventNameEdit = (EditText) findViewById(R.id.event_name_edit);
        saveEvent = (Button) findViewById(R.id.save_event);
        titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText("新增活动");
        timeManagerDB = TimeManagerDB.getInstance(this);
        event.setType(selectedType);
        saveEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(getEditName = eventNameEdit.getText().toString())) {
                    event.setName(getEditName);
                    if (timeManagerDB.saveEvent(event) > 0) {
                        startStartActivity();
                    } else {
                        showFailedDialog();
                    }
                } else {
                    Toast.makeText(AddEventActivity2.this, "活动名称不能为空",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 保存失败弹出对话框
     * */
    private void showFailedDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("添加失败");
        dialogBuilder.setMessage("该活动已经存在，无需添加。是否以直接开始该活动进行？");
        dialogBuilder.setCancelable(false);
        dialogBuilder.setPositiveButton("重新输入", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        dialogBuilder.setNegativeButton("开始该活动", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startStartActivity();
            }
        });
        dialogBuilder.show();
    }

    /**
     * 转到StartActivity
     */
    private void startStartActivity() {
        //将保存后的event取出，得到id
        Event eventStart = timeManagerDB.searchEventName(event.getName());
        Intent intent = new Intent(AddEventActivity2.this, StartEventActivity.class);
        intent.putExtra("event_id", eventStart.getId());
        intent.putExtra("event_name", eventStart.getName());
        startActivity(intent);
        finish();
    }
}
